<template>
	<view class="factory-manufacturing-page">
    <TopBar
        left-icon="/static/user.png"
        scan-icon="/static/saoyisao.png"
        menu-icon="/static/menu.png"
        @menu="toggleMenu"
    />

        <scroll-view scroll-y class="form-scroll">
            <view class="back-row" @click="goBack">
                <image class="back-arrow-icon" src="/static/left.png" mode="aspectFit"></image>
            </view>
			<view class="header">
				<text class="title">Factory Profile</text>
				<text class="subtitle">Section 4. Materials & Processes</text>
			</view>

            <!-- Manufacturing Processes -->
            <text class="section-title">Manufacturing Processes</text>
            <view class="section">
                <view class="process-list">
                    <view class="process-item" v-for="item in manufacturingProcesses" :key="item.name">
                        <view class="checkbox-row" @click="toggleCheckbox(item.name)">
                            <view class="checkbox" :class="{ checked: item.checked }">
                                <text class="checkmark" v-if="item.checked">✓</text>
                            </view>
                            <text class="process-label">{{ item.name }}</text>
                        </view>
                        <view class="switch-row">
                            <view class="switch-container">
                                <text class="switch-text">In-house</text>
                                <switch class="toggle-switch" :checked="item.external" @change="(e) => onProcessChange(item.name, e)" color="#333" />
                                <text class="switch-text">External</text>
                            </view>
                        </view>
                    </view>
                </view>
            </view>

			<button class="next-btn" @click="goToFactoryCertification">Next</button>
        </scroll-view>

        <AccountMenu
            v-if="showMenu"
            :style="{ '--anchor-top': '70px' }"
            @close="showMenu = false"
            @select="() => showMenu = false"
        />
	</view>
</template>

<script setup>
import TopBar from '@/components/TopBar.vue';
import AccountMenu from '@/components/AccountMenu.vue';
import { ref } from 'vue';

const goBack = () => {
    uni.navigateTo({
        url: '/pages/factory/factory-info'
    });
};
const showMenu = ref(false);
const toggleMenu = () => { showMenu.value = !showMenu.value; };

// 制造工艺数据
const manufacturingProcesses = ref([
    { name: 'Knitting / Weaving', checked: false, external: false },
    { name: 'Dyeing & Finishing', checked: false, external: false },
    { name: 'Garment Making (Cut & Sew)', checked: false, external: false },
    { name: 'Embroidery / Printing', checked: false, external: false },
    { name: 'Packaging & Trims', checked: false, external: false }
]);

// 切换复选框状态
const toggleCheckbox = (itemName) => {
    const item = manufacturingProcesses.value.find(item => item.name === itemName);
    if (item) {
        item.checked = !item.checked;
    }
};

// 开关变化处理
const onProcessChange = (itemName, e) => {
    const item = manufacturingProcesses.value.find(item => item.name === itemName);
    if (item) {
        item.external = e.detail.value;
    }
};

const goToFactoryCertification = () => {
    uni.navigateTo({
        url: '/pages/factory/factory-profile'
    });
};
</script>

<style lang="scss">
.factory-manufacturing-page { min-height: 100vh; background: linear-gradient(180deg, #d9f0ff 0%, #e9f9ed 100%); }
.form-scroll { margin-top: 60px; padding: 0 16px 80px 16px; box-sizing: border-box; }
.back-row { margin: 12px 0; width: 44px; height: 44px; display: flex; align-items: center; justify-content: center; }
.back-arrow-icon { width: 32px; height: 32px; }
.header { margin-bottom: 8px; }
.title { font-size: 22px; font-weight: 800; color: #0b1e2a; }
.subtitle { display: block; margin-top: 6px; color: #3b5560; font-size: 14px; }

.section-title { 
    font-size: 16px; 
    font-weight: 600; 
    color: #23323a; 
    margin: 16px 0 8px 0; 
    display: block; 
}
.section { 
    padding: 16px; 
    margin-bottom: 12px; 
}

.process-list { }
.process-item { 
    margin-bottom: 20px; 
    padding: 8px 0; 
}
.process-item:last-child { 
    margin-bottom: 0; 
}
.checkbox-row {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
}
.switch-row {
    display: flex;
    justify-content: flex-start;
}
.checkbox { 
    width: 16px; 
    height: 16px; 
    border: 2px solid #333; 
    border-radius: 3px; 
    margin-right: 10px; 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    background: #fff; 
}
.checkbox.checked { 
    background: #333; 
    border-color: #333; 
}
.checkmark { 
    color: #fff; 
    font-size: 10px; 
    font-weight: bold; 
}
.process-label { 
    color: #000; 
    font-size: 14px; 
}

.switch-container {
    display: flex;
    align-items: center;
    gap: 12px;
}
.switch-text {
    color: #000;
    font-size: 16px;
    font-weight: 500;
}
.toggle-switch { 
    transform: scale(0.8); 
}
/* 确保switch为黑色主题 */
.toggle-switch :deep(.uni-switch-input) {
    background-color: #333 !important;
    border-color: #333 !important;
}
.toggle-switch :deep(.uni-switch-input:before) {
    background-color: #333 !important;
}
.toggle-switch :deep(.uni-switch-input.uni-switch-input-checked) {
    background-color: #333 !important;
    border-color: #333 !important;
}
.toggle-switch :deep(.uni-switch-input.uni-switch-input-checked:before) {
    background-color: #fff !important;
}

.next-btn { 
    margin: 40px 8px 20px; 
    height: 44px; 
    background: #222; 
    color: #fff; 
    border-radius: 10px; 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    position: fixed;
    bottom: 20px;
    left: 8px;
    right: 8px;
}

/* PC宽屏时，让底部固定按钮跟随居中容器并限制最大宽度 */
@media (min-width: 768px) {
	.next-btn {
		left: 50%;
		right: auto;
		transform: translateX(-50%);
		width: 100%;
		max-width: 420px;
		z-index: 1001;
	}
}
</style>
